package com.ws.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.commonutils.R;
import com.ws.sta.client.UcenterClient;
import com.ws.sta.entity.StatisticsDaily;
import com.ws.sta.mapper.StatisticsDailyMapper;
import com.ws.sta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-12-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void createSta(String day) {
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated,day);
        StatisticsDaily daily = baseMapper.selectOne(wrapper);
        R register = ucenterClient.getCountRegister(day);
        if(daily==null){
            daily = new StatisticsDaily();
            daily.setRegisterNum((Integer)register.getData().get("cnt"));
            daily.setLoginNum(RandomUtils.nextInt(100, 200));
            daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
            daily.setCourseNum(RandomUtils.nextInt(100, 200));
            daily.setDateCalculated(day);
            baseMapper.insert(daily);
        }
        daily.setRegisterNum((Integer)register.getData().get("cnt"));
        baseMapper.updateById(daily);

    }

    @Override
    public Map<String, Object> getStaCnt(String type, String start, String end) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", start, end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> dataList = new ArrayList<>();
        for (StatisticsDaily daily : statisticsDailies) {
            dateList.add(daily.getDateCalculated());
            switch (type){
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }

        }
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        return map;
    }
}
